package vn.wallet.app.managedBean;

import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import vn.wallet.app.domain.User;
import vn.wallet.app.helper.AppUtils;
import vn.wallet.app.helper.exceptions.TechnicalException;

public abstract class DefaultBean {

	@ManagedProperty(value = "#{siteConfig}")
	SiteConfigBean siteConfig;

	protected abstract void setPageTitle(String str);

	protected abstract void saveItem();

	protected abstract void updateItem();

	protected abstract void deleteItem();

	/**
	 * 
	 * This method is for preparing data before showing to html Class inherite<br/>
	 * All data would be loaded by this method
	 * 
	 * @throws Exception
	 */
	public abstract void prepareData() throws Exception;

	public User currentUser;

	public final String OPTION_ALL = AppUtils.getResource("wallet.app.option.ALL");
	public final String OPTION_ACTIVE = AppUtils.getResource("wallet.app.option.ACTIVE");
	public final String OPTION_DISABLED = AppUtils.getResource("wallet.app.option.DISABLED");
	public final String OPTION_NOT_UPDATE = AppUtils.getResource("wallet.app.option.NotUpdate");

	public SelectItem[] booleanStatusOption;

	public DefaultBean() {
		currentUser = getCurrentUser();
	}

	public SelectItem[] getBooleanStatusOption() {
		booleanStatusOption = new SelectItem[3];
		booleanStatusOption[0] = new SelectItem(-1, OPTION_ALL);
		booleanStatusOption[1] = new SelectItem(1, OPTION_ACTIVE);
		booleanStatusOption[2] = new SelectItem(0, OPTION_DISABLED);

		return booleanStatusOption;
	}

	// ------------------------------------properties
	public User getCurrentUser() {
		try {
			currentUser = (User) FacesContext.getCurrentInstance()
					.getExternalContext().getSessionMap()
					.get(AppUtils.USER_LOGINED_SESSION_NAME);
		} catch (Exception ex) {
			throw new TechnicalException("User not found");
		}
		return currentUser;
	}

	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}

	public SiteConfigBean getSiteConfig() {
		return siteConfig;
	}

	public void setSiteConfig(SiteConfigBean siteConfig) {
		this.siteConfig = siteConfig;
	}
}
